package mmc.server.shell.ops.function;

import mmc.server.shell.OperationNotAcceptedException;
import mmc.server.shell.ShellException;
import mmc.server.shell.Util;
import mmc.server.shell.ops.Function;
import mmc.server.shell.ops.Operation;

public class AssertEquals extends Function {
	
	boolean isObject;
	
	Operation o1, o2;
	
    public AssertEquals(String text) throws ShellException {
        super(text, "assertequals", 2, false);
        
        o1 = arguments.getArgument(0);
        o2 = arguments.getArgument(1);
        
        if(o1.hasDoubleOutput()!=o2.hasDoubleOutput()){
        	throw new OperationNotAcceptedException(true, "cannot compare numerics to objects!");
        }
        
        isObject = !o1.hasDoubleOutput();
    }

    @Override
    public double execute() {
        throw new RuntimeException(this+" has no return value");
    }
    
    public Object executeComplex(){
    	if(isObject){
    		Object r1 = o1.executeComplex();
			Object r2 = o2.executeComplex();
			if(!r1.equals(r2))
    			throw new RuntimeException("Assertion failed: "+text+" : <"+r1+"> vs <"+r2+">");
    	}else{
    		double d1 = o1.execute();
			double d2 = o2.execute();
			if(Util.doubleEquals(d1, d2))
    			throw new RuntimeException("Assertion failed: "+text+" : <"+d1+"> vs <"+d2+">");
    	}
    	
    	return null;
    }
    
    public Class<?> getOutputType(){
    	return void.class;
    }
    
    public static String getDescription(){
    	return formatHelpString("assertEquals(o1,o2)","assert o1.equals(o2)");
    }
}
